<template>
	<div class="layout-padding w-full h-full mt-2">
		<div class="layout-padding-auto layout-padding-view">
			<el-row shadow="hover" v-show="showSearch" class="ml10">

			  <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
                    <el-form-item :label="t('aigcConversation.userName')" prop="userName">
                        <el-input v-model="state.queryForm.userName" :placeholder="t('aigcConversation.inputUserNameTip')"></el-input>
                    </el-form-item>
                    <el-form-item :label="t('aigcConversation.title')" prop="title">
                        <el-input v-model="state.queryForm.title" :placeholder="t('aigcConversation.inputTitleTip')"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="search" type="primary" @click="getDataList">{{ t('common.queryBtn') }}</el-button>
                        <el-button icon="Refresh" @click="resetQuery">{{ t('common.resetBtn') }}</el-button>
                    </el-form-item>
                </el-form>
			</el-row>
			<el-row>
				 <div class="mb8" style="width:100%">
                    <el-button icon="Plus" type="primary" @click="modelDialogRef.openDialog()" class="ml10" v-auth="'aigc_conversation_add'">{{ t('common.addBtn') }}</el-button>
                    <el-button plain :disabled="multiple" icon="Delete" type="primary" class="ml10" v-auth="'aigc_conversation_delete'" @click="handleDelete(selectObjs)">
                        {{ t('common.delBtn') }}
                    </el-button>
                    <right-toolbar
                        v-model:showSearch="showSearch"
                        :export="'aigc_conversation_export'"
                        @exportExcel="exportExcel"
                        class="ml10"
                        style="float: right; margin-right: 20px"
                        @queryTable="getDataList"/>
                </div>
			</el-row>
			 <el-table :data="state.dataList"
                    v-loading="state.loading"
                    style="width: 100%"
                    border
                    @selection-change="handleSelectionChange"
                    :cell-style="tableStyle.cellStyle"
                    :header-cell-style="tableStyle.headerCellStyle"
                >
                    <el-table-column type="selection" width="50" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.promptId')" prop="promptId" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.userId')" prop="userId" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.title')" prop="title" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.userName')" prop="userName" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.chatTotal')" prop="chatTotal" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.tokenUsed')" prop="tokenUsed" align="center"></el-table-column>
                    <el-table-column :label="t('aigcConversation.lastTime')" prop="lastTime" align="center"></el-table-column>
                    <el-table-column :label="t('common.action')" align="center" width="250">
                        <template #default="scope">
                            <el-button text type="primary" icon="edit-pen"  v-auth="'aigc_conversation_edit'" @click="editDialogRef.openDialog(scope.row.id)">{{ $t('common.editBtn') }}</el-button>
                            <el-button text icon="delete" v-auth="'aigc_conversation_delete'" @click="handleDelete([scope.row.id])">{{ $t('common.delBtn') }}</el-button>
                        </template>
                    </el-table-column>

                </el-table>
                <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
	</div>
</template>

<script setup lang="ts" name="aigcKnowledge">
   import { useI18n } from "vue-i18n";
import { delObj, pageList } from "/@/api/aigc/knowledge";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { BasicTableProps, useTable } from "/@/hooks/table";

const { t } = useI18n();

// 定义变量内容
const modelDialogRef = ref();
const editDialogRef = ref();
const queryRef = ref();
const showSearch = ref(true);
// 多选rows
const selectObjs = ref([]) as any;
// 是否可以多选
const multiple = ref(true);
//组件激活时加载
onActivated(()=>{
    getDataList();
});
//表格设置
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		userName:'',
        title:'',
	},
	pageList: pageList, // H
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	getDataList();
};

//多选事件
const handleSelectionChange=(objs:{id:string}[])=>{
    selectObjs.value = objs.map(({ id }) => id);
    multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
 //导出
 const exportExcel =()=>{
     downBlobFile('/aigc/conversation/export',state.queryForm,'aigcConversation.xlsx');
 }
</script>
